﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data;
using System.Data.SqlClient;

namespace DAO
{
    public class KhachHangDAO
    {
        public class SqlConnectionData
        {
            public static SqlConnection Connection()
            {
                SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=QLKS;Integrated Security=True");
                return conn;
            }
        }

        public static DataTable GetAll()
        {
            SqlConnection conn = SqlConnectionData.Connection();
            SqlCommand cmd = new SqlCommand("sp_KhachHangGetAll", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;

        }
        public static DataTable GetTheoMa(int MaKhachHang)
        {
            SqlConnection conn = SqlConnectionData.Connection();
            SqlCommand cmd = new SqlCommand("sp_GetTheoMa", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@MaKhachHang",SqlDbType.Int);
            cmd.Parameters["@MaKhachHang"].Value =MaKhachHang;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
        }
        public static DataTable MaKhachTheoTen(string TenKhachHang)
        {
            SqlConnection conn = SqlConnectionData.Connection();
            SqlCommand cmd = new SqlCommand("sp_MaTheoTen", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@TenKhachHang", SqlDbType.NVarChar,50);
            cmd.Parameters["@TenKhachHang"].Value = TenKhachHang;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
            //string sql = string.Format("Select MaKhachHang From KHACHHANG Where TenKhachHang like N'%" + ten + "%'");
            //DataTable dt = DataProvider.ExecuteQuery(sql);//cần store
            //return dt;
        }
        public static DataTable KhachTheoMa(int ma)
        {
            string sql = "Select kh.MaKhachHang, kh.TenKhachHang, lk.TenLoaiKhach, kh.CMND, kh.DiaChi From KHACHHANG kh, LOAIKHACHHANG lk where kh.MaLoaiKhach = lk.MaLoaiKhach and kh.MaKhachHang = " + ma + "";
            DataTable dt = DataProvider.ExecuteQuery(sql);
            return dt;
        }

        public static DataTable MaKTheoTen(string TenKhachHang)
        {
            SqlConnection conn = SqlConnectionData.Connection();
            SqlCommand cmd = new SqlCommand("sp_MaKhachTheoTen", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@TenKhachHang", SqlDbType.NVarChar, 50);
            cmd.Parameters["@TenKhachHang"].Value = TenKhachHang;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
        }

    }
}
